package dolf.objects;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.BasicStroke;

import dolf.objects.WorldObject;

public class Loadbar extends WorldObject implements java.io.Serializable{
								/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	//the bar is drawn as line
	private Point end;			//this point the vector leads to the end of the line
	private int width;
	private int length;
	private Color bg;
	private Color fg;
	private double angle;
	private int loadstate;		// int from 0 to 100 gives the % of the loading area
	
	public Loadbar(Point position, int length, int width, Color bg, Color fg){
		super(false);
		this.position = position;
		this.length = length;
		this.width = width;
		this.bg = bg;
		this.fg = fg;
		end = new Point(length, 0);
		angle = 0;
		loadstate = 0;
	}
	
	public void paint(Graphics2D g){
		Stroke tmp = g.getStroke();
		Color tmpc = g.getColor();
		g.setStroke(new BasicStroke( width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER ) );
		g.setColor(bg);
		g.drawLine(position.getIntX(), position.getIntY(), (position.getIntX() + end.getIntX()), (position.getIntY() + end.getIntY()));
		g.setColor(fg);
		g.drawLine(position.getIntX(), position.getIntY(), (int)(position.getIntX() + (end.getIntX()*loadstate/100)),(int)(position.getIntY() + (end.getIntY()*loadstate/100)));
		g.setStroke(tmp);
		g.setColor(tmpc);
	}
	
	public void increaseLoading(int val){
		this.loadstate += val;
		if(this.loadstate > 100){
			this.loadstate = 100;			
		}
	}
	
	public void decreaseLoading(int val){
		this.loadstate -= val;
		if(this.loadstate < 0){
			this.loadstate = 0;
		}
	}
	
	public void setLoadstate(int state){
		if(state > 100){
			this.loadstate = 100;
		} else {
			if(state < 0){
				this.loadstate = 0;
			} else {
				this.loadstate = state;
			}
		}
		
	}
	
	public void setWidth(int width){
		this.width = width;
	}
	
	public void setLength(int length){
		this.length = length;
	}
	
	public void setFg(Color fg){
		this.fg = fg;
	}
	
	public void setBg(Color bg){
		this.bg = bg;
	}
	
	public void setAngle(double angle){
		this.angle = angle * (-1);
		this.end.setX(length*Math.cos(this.angle/180*Math.PI));
		this.end.setY(length*Math.sin(this.angle/180*Math.PI));
	}
	
	public int getLoadstate(){
		return loadstate;
	}
	
	public int getWidth(){
		return this.width;
	}
	
	public int getLength(){
		return this.length;
	}
	
	public Color getFg(){
		return this.fg;
	}
	
	public Color getBg(){
		return this.bg;
	}
	
	public double getAngle(){
		return this.angle*(-1);
	}
	public Point getEnd(){
		return this.end;
	}
	
}
